// 引用百度地图微信小程序JSAPI模块 
var bmap = require('@/libs/bmap-wx.min.js'); 
var wxMarkerData = []; 
Page({ 
    data: { 
        markers: [], 
        latitude: '', 
        longitude: '', 
        placeData: [{}],
        keyWord:'',
        sendData:{},
        address:{
            postCode:null,
            province:null,
            city:null,
            town:null,
            detailAddress:null,
            fullAddress:null,
            areaCode:null,
        }
    }, 

    makertap: function(e) { 
        var that = this; 
        var id = e.markerId; 
        that.showSearchInfo(wxMarkerData, id);  
    }, 
    onLoad: function() { 
       this.createMap() 
    }, 
    bindKeyInput(){
        this.createMap() 
    },
    createMap() {
        var that = this;
        // 新建百度地图对象 
        var BMap = new bmap.BMapWX({ 
            ak: 'q4xeP2ndQ5VCrFX5fx5gYJ28Zn3vxVSb',
        }); 
        var fail = function (data) {
            console.log(data);
        }
        var success = function(data) { 
          var { originalData,wxMarkerData} = data; 
          var tempaddress = originalData.results.map(item=>{
                let obj = {}
                obj.title = item.name;
                obj.fullAddress = item.address;
                obj.province = item.province;
                obj.city = item.city;
                obj.town = item.area;
                obj.detailAddress = item.address.replace(item.province,'').replace(item.city,'').replace(item.area,'')
                return obj;
        })
        var tempproList =  wxMarkerData.map(item=>{
            let obj = {}
            obj.phone = item.telephone;
            obj.id = item.id;
            obj.checked = false
            return obj;
        })
        let address = tempaddress.map((item,index)=>{
            return{...item, ...tempproList[index]}
        })

        //设置marks宽度和高度
        let tempMarks = wxMarkerData.map(item=>{
            item.height = '30';
            item.width = '40'
            return item
        })
           
            let s =
            that.setData({ 
                address,
                markers: tempMarks ,
                latitude: tempMarks[0].latitude ,
                longitude: tempMarks[0].longitude 
            }); 
            console.log("wxMarkerData:",tempMarks);
        }
                // 发起POI检索请求 
        BMap.search({ 
            "query": this.data.keyWord, 
            fail: fail, 
             success: success ,

            // 此处需要在相应路径放置图片文件 
            iconPath: '/static/img/mark_red.png', 
            // 此处需要在相应路径放置图片文件 
            iconTapPath: '/static/img/mark_red.png' 
        });    
    },
    chooseAddr(){  
    let {sendData} = this.data
    wx.redirectTo({
        url:`/miniprogram/pre_loadModules/addressModule/pages/insertOrUpdateaddress/insertOrUpdateaddress?addr=${JSON.stringify(sendData)}`
    })

    },
    radioGroupBind(r){
        var that = this
        let i =  r.detail.value
        let {address} = this.data
          address.forEach(item => {
            if(item.id == i){
                this.setData({
                    sendData:item
                })
           }
        });
      }
})